<?php

namespace app\logic\yuql;


use app\api\model\yuql\Bargain;
use app\api\model\yuql\Login;
use app\services\yuql\RedisServices;

class BargainLogic
{
    /**
     * yuql
     * 用户砍价逻辑
     * @param $params
     * @throws \Exception
     */
    public static function userBargain($params)
    {
        if (empty($params)) throw new \Exception('没有此商品');
        $goodsOne = Bargain::goodsOne(['goods_id' => $params['goods_id']]);
        $userOne = Bargain::userOne(['id'=>$params['id']]);
        $begin_time = time();
        $end_Time = date('Y-m-d 23:59:59');
        $end_Time = strtotime($end_Time);
        $sec = BargainLogic::timeDiff($begin_time, $end_Time);
        if ($sec < 1) throw new \Exception('今日砍价时间结束');
        $redis = RedisServices::getInstance()->getRedis();
        $redis->select(4);
        $key = '2008a' . $goodsOne['goods_id'];
        $keys = $redis->get($key);
        if ($keys) {
            //假设每天限制最多砍价10次
            if ($keys < 5) {
                $redis->incrBy($key, 1);
                $barginNum = $redis->get($key);
                $bargainGoodsOne = Bargain::bargainGoodsOne(['product_id'=>$params['goods_id']]);
                $bargainMostPrice = $bargainGoodsOne['price'] - $bargainGoodsOne['end_cut'];
                if ($bargainMostPrice < $bargainGoodsOne['end_cut']){
                    $bargainMostPrice = $bargainGoodsOne['end_cut'];
                }
                $proportion = '0.' . mt_rand(1,99);
                $bargainPrice = $bargainMostPrice * $proportion;
                if ($keys == 4) $bargainPrice = 0;
                $userBargain = Bargain::bargainUpdate(['product_id' => $goodsOne['goods_id']], $barginNum, $bargainPrice);
                Bargain::bargainRecord([
                    'uid'=>$userOne['id'],
                    'bargain_id' => $params['goods_id'],
                    'bargain_user_id' => $params['id'],
                    'price' => $bargainGoodsOne['price'] - $bargainPrice,
                    'add_time' => time()
                ]);
            } else {
                throw new \Exception('超出每日砍价次数');
            }
        } else {
            //键值不存在,或用户第一次，或者过期
            $redis->set($key, 1);
            //如果第一次砍价是用户自己还是别人帮砍
            if ($params['bar_uid'] == $userOne['id']){
                $goodsPricePro = $goodsOne['price'] * 0.8;
            }else{
                $goodsPricePro = $goodsOne['price'] * 0.9;
            }
            $userBargain = Bargain::bargainSet([
                'product_id' => $goodsOne['goods_id'],
                'title' => $goodsOne['title'],
                'price' => $goodsPricePro,
                'bargain_num' => $redis->get($key),
                'start_time' => $begin_time,
                'stop_time' => $end_Time
            ]);
            Bargain::bargainRecord([
                'uid'=>$userOne['id'],
                'bargain_id' => $params['goods_id'],
                'bargain_user_id' => $params['id'],
                'price' => $goodsOne['price'] - $goodsPricePro,
                'add_time' => time()
            ]);
            return $userBargain;
        }
        return $userBargain;
    }

    /**
     * yuql
     * 获取当日离结束还有多长时间
     * @param $begin_time
     * @param $end_time
     * @return float|int
     */
    public static function timeDiff($begin_time, $end_time)
    {
        if ($begin_time < $end_time) {
            $starttime = $begin_time;
            $endtime = $end_time;;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //计算天数
        $timediff = $endtime - $starttime;
        $days = intval($timediff / 86400);
        //计算小时数
        $remain = $timediff % 86400;
        $hours = intval($remain / 3600);
        //计算分钟数
        $remain = $remain % 3600;
        $mins = intval($remain / 60);
        //计算秒数
        $secs = $remain % 60;
//         $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
        $s = ($hours * 3600) + ($mins * 60) + $secs;
        return $s;
    }

}